﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace DomainTool
{
    class FindDomainUtils
    {

        public static List<string> findDomainsFromFile(String filePath, String suffixReg)
        {
            List<string> domains = new List<string>();
            Regex domainReg = new Regex(@"^([a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(" + suffixReg + "))$");
            StreamReader objReader = new StreamReader(filePath);
            string sLine = objReader.ReadLine();
            while (sLine != null)
            {
                if (domainReg.IsMatch(sLine))
                {
                    domains.Add((domainReg.Matches(sLine)[0].Groups[1]).ToString());
                }
                sLine = objReader.ReadLine();
            }
            objReader.Close();
            return domains;
        }
        public static List<string> length3Domain(List<string> domains, string suffixReg, int length = 10)
        {
            List<string> word4Domain = new List<string>();
            Regex domainReg = new Regex(@"^[a-z0-9]{1,3}\.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (domainReg.IsMatch(domain))
                {
                    word4Domain.Add(domain);
                }
            }
            return word4Domain;
        }
        public static List<string> length4Num(List<string> domains, string suffixReg, int length = 10)
        {
            List<string> word4Domain = new List<string>();
            Regex domainReg = new Regex(@"^[0-9]{4}\.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (domainReg.IsMatch(domain))
                {
                    word4Domain.Add(domain);
                }
            }
            return word4Domain;
        }
        public static List<string> length4Char(List<string> domains, string suffixReg, int length = 10)
        {
            List<string> word4Domain = new List<string>();
            Regex domainReg = new Regex(@"^[a-z]{4}\.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (domainReg.IsMatch(domain))
                {
                    word4Domain.Add(domain);
                }
            }
            return word4Domain;
        }
        public static List<string> startWith51(List<string> domains, string suffixReg, int length = 10)
        {
            List<string> resultDomain = new List<string>();
            Regex domainReg = new Regex(@"^51[a-z]+\.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (domainReg.IsMatch(domain))
                {
                    resultDomain.Add(domain);
                }
            }
            return resultDomain;
        }
        public static List<string> startWithChina(List<string> domains, string suffixReg, int length = 10)
        {
            List<string> resultDomain = new List<string>();
            Regex domainReg = new Regex(@"^china[a-z]+\.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (domainReg.IsMatch(domain))
                {
                    resultDomain.Add(domain);
                }
            }

            return resultDomain;
        }
        public static List<string> pinyin(List<string> domains, string suffixReg, int pinLength, int length = 10)
        {
            string pinL = pinLength == 2 ? "1,2" : "3";
            List<string> resultDomain = new List<string>();
            Regex pinyinReg = new Regex("^(ling|yi|ding|zheng|kao|qi|shang|xia|wan|mo|zhang|san|ji|bu|fou|yu|mian|gai|chou|zhuan|qie|ju|pi|shi|qiu|bing|ye|cong|dong|si|cheng|diu|liang|you|yan|sang|shu|jiu|ge|ya|qiang|pan|zhong|jie|feng|guan|chuan|chan|lin|zhuo|zhu|dian|dan|wei|jing|li|pie|fu|ai|nai|tuo|me|ma|yao|zhi|wu|zha|hu|fa|le|yue|ping|pang|qiao|guai|sheng|yin|mie|nie|xi|xiang|nang|jia|mai|luan|ru|sha|na|gan|qian|gui|jun|jue|liao|er|chu|kui|yun|sui|gen|geng|xie|tou|wang|kang|ta|jiao|hai|heng|mu|ting|qin|qing|bo|lian|duo|ren|shen|she|ze|jin|pu|ba|reng|fo|lun|cang|zi|zai|xian|cha|hong|tong|di|dai|chao|chang|sa|men|yang|jian|fen|fang|pei|diao|dun|wen|xin|xiu|bei|chen|tang|huo|hui|kuai|cui|zu|che|xun|chi|xuan|nao|nu|bai|gu|ni|ban|xu|zhou|qu|ci|beng|ga|qia|tian|han|bi|shao|zuo|ti|ben|zhan|he|tu|die|gou|ning|yong|wa|ka|huai|lao|ming|quan|tiao|xing|kan|kai|lai|kua|guang|mi|an|lu|mou|gong|lu|zhen|ce|zhai|chai|nong|hou|jiong|cuo|nan|hao|bian|pian|tui|cu|e|kuang|ku|zun|juan|lang|hun|pai|su|bao|lia|xiao|biao|fei|zan|zong|dao|tan|chui|peng|kong|wo|luo|song|leng|cai|ying|nuan|chun|ruo|huang|duan|ou|za|lou|sou|yuan|rong|bang|shan|suo|bin|nuo|lei|zao|ao|piao|man|deng|fan|tie|seng|zhuang|jiang|min|sai|dang|tai|lan|meng|neng|qiong|lie|du|teng|long|rang|zuan|r|xiong|chong|zhao|dui|ke|dou|mao|nei|liu|shou|ran|gang|gua|zui|kou|mei|zhun|cou|chuang|cun|hua|bie|pao|shua|xue|la|pou|po|tuan|cuan|keng|weng|shuo|tao|da|zang|suan|nian|shuai|ang|que|zhe|can|cen|shuang|zhui|rui|mang|a|tun|hang|shun|chuo|gao|ne|m|guo|pen|lo|pin|ha|yo|o|miu|n|ng|huan|nou|ken|chuai|pa|se|re|miao|cao|hei|dia|de|dei|zhuai|ceng|ca|nin|kun|ri|lue|sao|ruan|zeng|qun|wai|en|zou|nu|niu|kuo|rao|niang|rou|niao|nen|sun|kuan|shui|sen|hen|te|tei|zen|zhua|shuan|gun|shai|run|e|nue|gei|shei|ei|zei|lue|eng){" + pinL + "}.(" + suffixReg + ")$");
            foreach (string domain in domains)
            {
                if (domain.Length > length + 4)
                    continue;
                if (pinyinReg.IsMatch(domain))
                {
                    resultDomain.Add(domain);
                }
            }
            return resultDomain;

        }
    }
}
